package com.qf.czh.controller;

import com.github.pagehelper.PageInfo;
import com.qf.czh.model.RespBean;
import com.qf.czh.model.RespPageBean;
import com.qf.czh.service.LeaveService;
import com.qf.czh.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    LeaveService leaveService;
    RespBean respBean;


    @GetMapping("/user/page")
    @RequiresPermissions("admin:all")
    public String user(){
        return "user/usersList";
    }

    @GetMapping("/user/data")
    @ResponseBody
    public RespPageBean UserData(String page, String rows, String sort , String order, String realname){
        int p = Integer.parseInt(page);
        int r = Integer.parseInt(rows);
        PageInfo pageInfo =  userService.getUserByPage(p, r, sort, order,realname );
        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setTotal(pageInfo.getTotal());
        respPageBean.setRows(pageInfo.getList());
        return respPageBean;
    }

    @PostMapping("/user/add")
    @ResponseBody
    public RespBean addUser(String username , String realname , String gender , String birthday,String department , String rank){
        Integer integer = userService.addUser(username, realname, gender, birthday,department,rank);
        if (integer == 1){
            respBean = RespBean.ok("添加成功");
        }else {
            respBean = RespBean.error("添加失败");
        }
        return respBean;
    }
//
    @PostMapping("/user/update")
    @ResponseBody
    public RespBean editUser(Integer id ,String username ,String realname , String gender , String birthday , String department,String rank){
        Integer integer = userService.updateUser(id , username,realname,gender,birthday,department ,rank);
        if (integer == 1){
            respBean = RespBean.ok("更新成功");
        }else {
            respBean = RespBean.error("更新失败");
        }
        return respBean;
    }
//
    @PostMapping("/user/delete")
    @ResponseBody
    public RespBean deleteUser(@RequestParam("ids[]") String[] ids){
        Integer result = userService.deleteUser(ids);
        if (result == 1){
            respBean = RespBean.ok("删除成功");
        }else {
            respBean = RespBean.error("删除失败");
        }
        return respBean;
    }
}
